package org.acghub.mtdb.xmlgen.visitor;

import org.acghub.mtdb.common.Bean;
import org.acghub.mtdb.common.Type;
import org.acghub.mtdb.common.Var;
import org.acghub.mtdb.common.Visitor;
import org.acghub.mtdb.common.type.TypeBaseCollection;
import org.acghub.mtdb.common.type.TypeBaseMap;
import org.acghub.mtdb.common.type.TypeBasePrimitives;

import java.io.PrintStream;

/**
 * @author zhouhe
 */
public class VarOnConstructorVisitor implements Visitor {

    private final Var var;

    private final PrintStream ps;

    private final String prefix;

    public VarOnConstructorVisitor(Var var, PrintStream ps, String prefix) {
        this.var = var;
        this.ps = ps;
        this.prefix = prefix;
    }

    private void makePrimitives(TypeBasePrimitives type) {
        ps.println(prefix + "this." + var.getName() + " = " + var.getDefaultValue() + ";");
    }

    private void makeBean(Bean type) {
        ps.println(prefix + "this." + var.getName() + " = new " + type.javaImplType() + "(this, \"" + var.getName() + "\");");
    }

    private void makeContainer(Type type) {
        ps.println(prefix + "this." + var.getName() + " = new " + type.javaImplType() + ";");
    }


    @Override
    public void visit(TypeBasePrimitives type) {
        makePrimitives(type);
    }


    @Override
    public void visit(TypeBaseCollection type) {
        makeContainer(type);
    }

    @Override
    public void visit(TypeBaseMap type) {
        makeContainer(type);
    }

    @Override
    public void visit(Bean bean) {
        makeBean(bean);
    }
}
